package cn.sieis.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sieis.pojo.Application;
import cn.sieis.pojo.Contest;
import cn.sieis.pojo.Level;
import cn.sieis.service.application.ApplicationService;
import cn.sieis.service.contest.ContestService;
import cn.sieis.service.level.LevelService;
import cn.sieis.tools.Constants;
import cn.sieis.tools.PageSupport;

@Controller
@RequestMapping("/back/contest")
public class ContestController {

	@Resource
	private ContestService contestService;

	@Resource
	private ApplicationService applicationService;

	@Resource
	private LevelService levelService;

	@RequestMapping(value = "/list.html")
	public String getContestList(Model model, @RequestParam(value = "queryConName", required = false) String queryConName, @RequestParam(value = "queryConApplication", required = false) String queryConApplication, @RequestParam(value = "queryConLevel", required = false) String queryConLevel, @RequestParam(value = "pageIndex", required = false) String pageIndex) {
		Integer _queryConApplication = null;
		Integer _queryConLevel = null;
		List<Contest> contestList = null;
		List<Application> applicationList = null;
		List<Level> levelList = null;
		int pageSize = Constants.pageSize;
		int currentPageNo = 1;
		if (queryConName == null) {
			queryConName = "";
		}
		if (queryConApplication != null && !queryConApplication.equals("")) {
			_queryConApplication = Integer.parseInt(queryConApplication);
		}
		if (queryConLevel != null && !queryConLevel.equals("")) {
			_queryConLevel = Integer.parseInt(queryConLevel);
		}

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (NumberFormatException e) {
				return "redirect:/syserror.html";
			}
		}

		int totalCount = 0;
		try {
			totalCount = contestService.getContestCount(queryConName, _queryConApplication, _queryConLevel);
		} catch (Exception e) {
			e.printStackTrace();
		}
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
		try {
			contestList = contestService.getContestList(queryConName, _queryConApplication, _queryConLevel, currentPageNo, pageSize);
			applicationList = applicationService.getApplicationList();
			levelList = levelService.getLevelList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("contestList", contestList);
		model.addAttribute("applicationList", applicationList);
		model.addAttribute("levelList", levelList);
		model.addAttribute("queryConName", queryConName);
		model.addAttribute("queryConApplication", queryConApplication);
		model.addAttribute("queryConLevel", queryConLevel);
		model.addAttribute("totalPageCount", totalPageCount);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("currentPageNo", currentPageNo);
		return "back/contestlist";
	}

	@RequestMapping(value = "/add.html", method = RequestMethod.GET)
	public String addContest(@ModelAttribute("contest") Contest contest) {
		return "back/contestadd";
	}

	@RequestMapping(value = "/addsave.html", method = RequestMethod.POST)
	public String addContestSave(Contest contest, HttpSession session, HttpServletRequest request) {
		boolean flag = true;
		if (flag) {
			try {
				if (contestService.addContest(contest)) {
					return "redirect:/back/contest/list.html";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "back/contestadd";
	}

	@RequestMapping(value = "/modifycontest/{id}", method = RequestMethod.GET)
	public String getContestById(@PathVariable String id, Model model, HttpServletRequest request) {
		Contest contest = new Contest();
		try {
			contest = contestService.getContestById(Integer.parseInt(id));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute(contest);
		return "back/contestmodify";
	}

	@RequestMapping(value = "/modifysave.html", method = RequestMethod.POST)
	public String modifyContestSave(Contest contest, HttpSession session, HttpServletRequest request) {
		boolean flag = true;
		if (flag) {
			try {
				if (contestService.modify(contest)) {
					return "redirect:/back/contest/list.html";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "back/contestmodify";
	}

	@RequestMapping(value = "/delcontest.html", method = RequestMethod.GET)
	public String delContest(@RequestParam("id") String id) {
		try {
			if (contestService.deleteContestById(Integer.parseInt(id))) {
				System.out.println("ɾ���ɹ�");
			} else {
				System.out.println("ɾ��ʧ��");
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:/back/contest/list.html";
	}

	@RequestMapping(value = "/conlist.html")
	public String getConList(Model model, @RequestParam(value = "queryConName", required = false) String queryConName, @RequestParam(value = "queryConApplication", required = false) String queryConApplication, @RequestParam(value = "queryConLevel", required = false) String queryConLevel, @RequestParam(value = "pageIndex", required = false) String pageIndex) {
		Integer _queryConApplication = null;
		Integer _queryConLevel = null;
		List<Contest> contestList = null;
		List<Application> applicationList = null;
		List<Level> levelList = null;
		// ����ҳ������
		int pageSize = Constants.pageSize;
		// ��ǰҳ��
		int currentPageNo = 1;

		if (queryConName == null) {
			queryConName = "";
		}
		if (queryConApplication != null && !queryConApplication.equals("")) {
			_queryConApplication = Integer.parseInt(queryConApplication);
		}
		if (queryConLevel != null && !queryConLevel.equals("")) {
			_queryConLevel = Integer.parseInt(queryConLevel);
		}

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (NumberFormatException e) {
				return "redirect:/syserror.html";
			}
		}

		// ����������
		int totalCount = 0;
		try {
			totalCount = contestService.getContestCount(queryConName, _queryConApplication, _queryConLevel);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// ��ҳ��
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		// ������ҳ��βҳ
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
		try {
			contestList = contestService.getConList(queryConName, _queryConApplication, _queryConLevel);
			applicationList = applicationService.getApplicationList();
			levelList = levelService.getLevelList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("contestList", contestList);
		model.addAttribute("applicationList", applicationList);
		model.addAttribute("levelList", levelList);
		model.addAttribute("queryConName", queryConName);
		model.addAttribute("queryConApplication", queryConApplication);
		model.addAttribute("queryConLevel", queryConLevel);
		model.addAttribute("totalPageCount", totalPageCount);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("currentPageNo", currentPageNo);
		return "back/contestlist";
	}

	@RequestMapping(value = "/viewcontest/{id}", method = RequestMethod.GET)
	public String getContestByIdView(@PathVariable String id, Model model, HttpServletRequest request) {
		Contest contest = new Contest();
		try {
			contest = contestService.getContestById(Integer.parseInt(id));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute(contest);
		return "back/contestview";
	}

}
